﻿#include "ssTinyxmlExpand.h"


namespace StarSeeker
{

ssTinyxmlExpand::ssTinyxmlExpand(void)
{
	m_pXmlDoc = 0;
	m_pRootNode = 0;
	m_pCurrentNode = 0;
	m_firstLoop =true;
	m_pAttribute = 0;
	m_pSubElement = 0;
}

ssTinyxmlExpand::~ssTinyxmlExpand(void)
{
	if (m_pXmlDoc)
	{
		m_pXmlDoc->Clear();
		delete m_pXmlDoc;
		m_pXmlDoc = 0;
	}
}

void ssTinyxmlExpand::Clear()
{
	if (m_pXmlDoc)
	{
		m_pXmlDoc->Clear();
		delete m_pXmlDoc;
		m_pXmlDoc = 0;
	}
	m_pRootNode = 0;
	m_pCurrentNode = 0;
	m_firstLoop =true;
	m_pAttribute = 0;
	m_pSubElement = 0;
}

bool ssTinyxmlExpand::LoadXmlFromFile( const char * pPath, TiXmlEncoding type )
{
	if (m_pXmlDoc)
	{
		m_pXmlDoc->Clear();
		delete m_pXmlDoc;
		m_pXmlDoc = 0;
	}

	m_pXmlDoc = new TiXmlDocument();  
	if (!m_pXmlDoc->LoadFile(pPath, type))
	{
		m_pXmlDoc->Clear();
		delete m_pXmlDoc;
		m_pXmlDoc = 0;
		return false;
	}
	//m_pXmlDoc->Print();
	m_pRootNode = m_pXmlDoc->RootElement();
	m_pCurrentNode = m_pRootNode;
	m_firstLoop =true;
	m_pAttribute = 0;
	m_pSubElement = 0;

	return m_pRootNode!=0;
}
//从内存读入
bool ssTinyxmlExpand::LoadXmlFromString(const char * str, TiXmlEncoding type)
{
	if (m_pXmlDoc)
	{
		m_pXmlDoc->Clear();
		delete m_pXmlDoc;
		m_pXmlDoc = 0;
	}
	m_pXmlDoc = new TiXmlDocument();
	m_pXmlDoc->Parse(str, 0, type);
	if (m_pXmlDoc->Error())
	{
		printf("xml parse error: %s\n", m_pXmlDoc->ErrorDesc());
		return false;
	}
	m_pRootNode = m_pXmlDoc->RootElement();
	m_pCurrentNode = m_pRootNode;
	m_firstLoop =true;
	m_pAttribute = 0;
	m_pSubElement = 0;

	return m_pRootNode!=0;
}
//获取子节点
TiXmlElement * ssTinyxmlExpand::GetChildNodeByName(char * pName)
{
	if (m_pCurrentNode)
	{
		m_pCurrentNode = m_pCurrentNode->FirstChildElement(pName);
	}
	m_firstLoop =true;
	m_pAttribute = 0;
	return m_pCurrentNode;
}
//获取第一个子节点
TiXmlElement * ssTinyxmlExpand::GetFristChildNode()
{
	if (m_pCurrentNode)
	{
		m_pCurrentNode = m_pCurrentNode->FirstChildElement();
	}
	m_firstLoop =true;
	m_pAttribute = 0;
	return m_pCurrentNode;
}
//按顺序获取下一个子节点
TiXmlElement * ssTinyxmlExpand::GetNextChildNode()
{
	if (m_pCurrentNode)
	{
		m_pCurrentNode = m_pCurrentNode->NextSiblingElement();
	}
	m_firstLoop =true;
	return m_pCurrentNode;
}
//循环获取子节点
TiXmlElement * ssTinyxmlExpand::GetChildNode()
{
	if (m_pCurrentNode && m_firstLoop)
	{
		m_pCurrentNode = m_pCurrentNode->FirstChildElement();
		m_firstLoop = false;
	}
	else if (m_pCurrentNode && !m_firstLoop)
	{
		m_pCurrentNode = m_pCurrentNode->NextSiblingElement();
	}
	else
	{
		m_pCurrentNode = 0;
	}
	return m_pCurrentNode;
}
//指定节点
void ssTinyxmlExpand::SetCurrentNode(TiXmlElement * pElement)
{
	m_pCurrentNode = pElement;
}
//回到根节点
void ssTinyxmlExpand::BackToRoot()
{
	m_pCurrentNode = m_pRootNode;
}
//获取第一个属性
TiXmlAttribute * ssTinyxmlExpand::GetFristAttribute(TiXmlElement * pElement)
{
	if (pElement)
	{
		m_pAttribute = pElement->FirstAttribute();
	}
	else if (m_pCurrentNode)
	{
		m_pAttribute = m_pCurrentNode->FirstAttribute();
	}
	else
	{
		m_pAttribute = 0;
	}
	return m_pAttribute;
}
//按顺序获取下一个属性
TiXmlAttribute * ssTinyxmlExpand::GetNextAttribute()
{
	if (m_pAttribute)
	{
		m_pAttribute = m_pAttribute->Next();
		return m_pAttribute;
	}
	return 0;
}
//按顺序获取元素
TiXmlAttribute * ssTinyxmlExpand::GetAttribute(TiXmlElement * pElement)
{
	if (!m_pAttribute && pElement)
	{
		m_pAttribute = pElement->FirstAttribute();
	}
	else if (!m_pAttribute && m_pCurrentNode)
	{
		m_pAttribute = m_pCurrentNode->FirstAttribute();
	}
	else if (m_pAttribute)
	{
		m_pAttribute = m_pAttribute->Next();
	}
	else
	{
		m_pAttribute = 0;
	}
	return m_pAttribute;
}

const char* ssTinyxmlExpand::GetElementName(TiXmlElement * pElement)
{
	if (pElement)
		return pElement->Value();
	else
		return m_pCurrentNode->Value();
}

const char* ssTinyxmlExpand::GetElementText(TiXmlElement * pElement)
{
	if (pElement)
		return pElement->GetText();
	else
		return m_pCurrentNode->GetText();
}

const char* ssTinyxmlExpand::GetAttributeName()
{
	return m_pAttribute->Name();
}

int ssTinyxmlExpand::GetIntAttribute()
{
	return m_pAttribute->IntValue();
}

double ssTinyxmlExpand::GetDoubleAttribute()
{
	return m_pAttribute->DoubleValue();
}

const char* ssTinyxmlExpand::GetStringAttribute()
{
	return m_pAttribute->Value();
}

}
